package org.hackystat.projectbrowser.page.setaction;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.hackystat.ambientdevice.server.jaxb.Project;
import org.hackystat.ambientdevice.server.jaxb.Action;
import org.hackystat.ambientdevice.server.jaxb.TriggerActionPair;
import org.hackystat.projectbrowser.ProjectBrowserSession;

/**
 * The class form that edits an action name.
 * 
 * @author Edward Kim
 * 
 */
public class EditActionNameTable extends StatelessForm {

  /** Supports serialization. */
  private static final long serialVersionUID = 1L;

  private String oldTAPid;
  private String tempActionName;
  private String oldActionName;
  private String projectName;
  private PageParameters responseParam = new PageParameters();

  /**
   * The form that edits an action name.
   * 
   * @param id A String passed on to super (not really needed).
   * @param tapId The id of the current trigger action pair.
   * @param projectName The name of the current project.
   * @param oldActionName The name of the current action.
   */
  public EditActionNameTable(String id, String tapId, String projectName, String oldActionName) {
    super(id);
    this.oldTAPid = tapId;
    this.projectName = projectName;
    this.oldActionName = oldActionName;
    responseParam.add("projectName", this.projectName);
    responseParam.add("tapName", this.oldTAPid);

    setModel(new CompoundPropertyModel(this));
    add(new TextField("tempActionName"));
  }

  /**
   * When the form is submiting, check for errors and try to create a new project.
   */
  @Override
  public void onSubmit() {
    List<Action> actions = new ArrayList<Action>();
    Project project = ProjectBrowserSession.get().getProjectTypeXMLByNameId(this.projectName);
    List<TriggerActionPair> pairs = project.getTriggerActionPairs().getTriggerActionPair();
    for (TriggerActionPair pair : pairs) {
      if (pair.getId().equals(this.oldTAPid)) {

        if (pair.getNegativeActions() == null && pair.getPositiveActions() == null) {
          System.out.println("There are not Trigger/Action Pairs.");
        }
        else if (pair.getPositiveActions() == null && pair.getNegativeActions() != null) {
          actions.addAll(pair.getNegativeActions().getAction());
        }

        else if (pair.getNegativeActions() == null && pair.getPositiveActions() != null) {
          actions.addAll(pair.getPositiveActions().getAction());
        }
        else {
          actions.addAll(pair.getNegativeActions().getAction());
          actions.addAll(pair.getPositiveActions().getAction());
        }

        for (Action tempAction : actions) {
          if (tempAction.getId().equals(this.oldActionName)) {
            tempAction.setId(tempActionName);
          }
        }

      }
    }

    // set response page to go back to chooseTAP
    setResponsePage(ChooseAction.class, responseParam);
  }

  /**
   * The setter for the temp action name.
   * 
   * @param tempActionName The temp Action name.
   */
  public void setTempActionName(String tempActionName) {
    this.tempActionName = tempActionName;
  }

  /**
   * The getter for the temp action name.
   * 
   * @return The temp action name.
   */
  public String getTempActionName() {
    return this.tempActionName;
  }

}
